package com.wlf.config;

import io.netty.channel.nio.NioEventLoopGroup;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.client.codec.Codec;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ClassUtils;

@Configuration
public class RedissonConfig {

    /**
     * redis链接地址
     */
    @Value("${redisson.address}")
    private String address;

    /**
     * 当前处理核数量 * 2
     */
    @Value("${redisson.thread}")
    private Integer thread;

    /**
     * 指定编解码
     */
    @Value("${redisson.codec}")
    private String codec;

    /**
     * 命令失败重试次数，默认值:3
     */
    @Value("${redisson.retryAttempts}")
    private Integer retryAttempts;

    /**
     * 数据库编号，默认值：0
     */
    @Value("${redisson.database}")
    private Integer database;

    /**
     * redis 连接密码
     */
    @Value("${redisson.password}")
    private String password;

    /**
     * 锁定时间
     */
    public static int lockTime;

    @Bean(destroyMethod = "shutdown")
    RedissonClient redisson() throws Exception {
        Config config = new Config();
        config.useSingleServer().setAddress(address)
                .setDatabase(database)
                .setPassword(password)
                .setRetryAttempts(retryAttempts);
        Codec temp=(Codec) ClassUtils.forName(codec, ClassUtils.getDefaultClassLoader()).newInstance();
        config.setCodec(temp);
        config.setThreads(thread);
        config.setEventLoopGroup(new NioEventLoopGroup());
        return Redisson.create(config);
    }
}
